<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="户号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="户名" prop="userName">
        <el-input
          v-model="queryParams.meterNo"
          placeholder="请输入户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充值时间" v-if="false">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="false">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['revenue:rechargeRecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['revenue:rechargeRecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['revenue:rechargeRecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['revenue:rechargeRecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rechargeRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="false"/>
      <el-table-column label="户号" align="center" prop="userId" />
      <el-table-column label="户名" align="center" prop="userName" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="欠费金额" align="center" prop="amountDue" />
      <el-table-column label="最早欠费日期" align="center" prop="adviceDate" />
      <el-table-column label="逾期天数" align="center" prop="dueDate" />
      <el-table-column label="欠费状态" align="center" prop="billStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bill_status" :value="scope.row.billStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSms(scope.row)"
            v-hasPermi="['revenue:waterUserCharge:sms']"
          >发送短信</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充值记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="充值订单号" prop="ordersn">
          <el-input v-model="form.ordersn" placeholder="请输入充值订单号" />
        </el-form-item>
        <el-form-item label="户号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入户号" />
        </el-form-item>
        <el-form-item label="表索引号" prop="meterId">
          <el-input v-model="form.meterId" placeholder="请输入表索引号" />
        </el-form-item>
        <el-form-item label="表号" prop="meterNo">
          <el-input v-model="form.meterNo" placeholder="请输入表号" />
        </el-form-item>
        <el-form-item label="充值序号" prop="ser">
          <el-input v-model="form.ser" placeholder="请输入充值序号" />
        </el-form-item>
        <el-form-item label="充值金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入充值金额" />
        </el-form-item>
        <el-form-item label="充值操作时间" prop="chargeDate">
          <el-date-picker clearable
            v-model="form.chargeDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择充值操作时间">
          </el-date-picker>
        </el-form-item>
<!--        <el-form-item label="充值结果" prop="chargeResult">
          <el-input v-model="form.chargeResult" placeholder="请输入充值结果" />
        </el-form-item>-->
        <el-form-item label="充值到表成功时间" prop="topUpDate">
          <el-date-picker clearable
            v-model="form.topUpDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择充值到表成功时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="充值方式" prop="topUpType">
          <el-select v-model="form.topUpType" placeholder="请选择充值方式">
            <el-option
              v-for="dict in dict.type.top_up_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="充值机构代码" prop="orgCode">
          <el-input v-model="form.orgCode" placeholder="请输入充值机构代码" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="是否打印" prop="isPrint">
          <el-select v-model="form.isPrint" placeholder="请选择是否打印">
            <el-option
              v-for="dict in dict.type.custom_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="充值类型" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择充值类型">
            <el-option
              v-for="dict in dict.type.pay_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作员" prop="oper">
          <el-input v-model="form.oper" placeholder="请输入操作员" />
        </el-form-item>
        <el-form-item label="返回请求ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入返回请求ID" />
        </el-form-item>
        <el-form-item label="结果描述">
          <editor v-model="form.resultContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRechargeRecord, getRechargeRecord, sendSms} from "@/api/revenue/arrears";

export default {
  name: "RechargeRecord",
  dicts: ['pay_type', 'top_up_type', 'custom_yes_no', 'bill_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值记录表格数据
      rechargeRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      printVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        meterNo: null,
        topUpDate: null,
        topUpType: null,
        state: null,
        payType: null,
      },
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const userId = this.$route.params && this.$route.params.userId;
    this.queryParams.userId = userId;
    this.getList();
  },
  computed: {
  },
  methods: {
    /** 查询充值记录列表 */
    getList() {
      this.loading = true;
      listRechargeRecord(this.addDateRange(this.queryParams,this.dateRange)).then(response => {
        this.rechargeRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        meterNo: null,
        chargeDate: null,
        chargeResult: null,
        topUpDate: null,
        topUpType: null,
        state: null,
        payType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充值记录";
    },
    handlePrint(row){
      this.printVisible = true;
      this.invoice.items = [{ name: '用水缴费', unit: '元', quantity: 1, amount: row.money }]
    },
    /** 发送短信 */
    handleSms(row) {
      const userName = row.userName
      const contactInfo = row.contactInfo
      this.form = {
        userName: userName,
        contactInfo: contactInfo
      }
      sendSms(this.form).then(response => {
        this.$modal.msgSuccess("成功发送短信");
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRechargeRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充值记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRechargeRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRechargeRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除充值记录编号为"' + ids + '"的数据项？').then(function() {
        return delRechargeRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('revenue/rechargeRecord/export', {
        ...this.queryParams
      }, `rechargeRecord_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.invoice-container {
  padding: 20px;
  background-color: #fff;
}
.invoice-header {
  text-align: center;
  margin-bottom: 20px;
  border-bottom: 2px solid #409EFF;
  padding-bottom: 10px;
}
/* 表格边框优化 */
.invoice-table {
  width: 100%;
  margin-top: 20px;
  border-collapse: collapse;
}
.invoice-table th,
.invoice-table td {
  border: 1px solid #000;
  padding: 8px 10px;
  text-align: center;
}
.invoice-table th {
  background-color: #f5f7fa;
}

.invoice-section {
  margin-bottom: 15px;
}
.invoice-total {
  text-align: right;
  margin-top: 20px;
  font-weight: bold;
}
.invoice-remark {
  margin-top: 15px;
}
.invoice-footer {
  margin-top: 30px;
  text-align: center;
  border-top: 1px dashed #ddd;
  padding-top: 10px;
}

/* 打印样式优化 */
@media print {
  body * {
    visibility: hidden;
  }
  #printArea, #printArea * {
    visibility: visible;
  }
  #printArea {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
  }
  .invoice-table {
    border: 1px solid #000 !important;
  }
  .invoice-table th,
  .invoice-table td {
    border: 1px solid #000 !important;
    color: #000 !important;
    background: transparent !important;
  }
  .el-dialog {
    position: static !important;
    width: 100% !important;
    margin: 0 !important;
  }
}
</style>
